﻿using FairyGUI;

/// <summary>
/// UI根控制器
/// </summary>
public class UIRootController : BaseController {

    /// <summary>
    /// 设计宽度
    /// </summary>
    public int _designWidth;

    /// <summary>
    /// 设计高度
    /// </summary>
    public int _designHeight;

    /// <summary>
    /// 当前UI界面
    /// </summary>
    UIBaseWindow _curUI;
    /// <summary>
    /// 网络掩码
    /// </summary>
    GComponent _netMask;
    /// <summary>
    /// 当前顶
    /// </summary>
    int _curTop;

	GComponent _mainUI;
	GComponent _popupUI;

    public UIBaseWindow curUI { get { return _curUI; } }

    static UIRootController _inst;
    public static UIRootController inst { get { return _inst; } }

    protected override void InitData()
    {
        _curTop = 1;

        _inst = this;
    }

    protected override void InitView()
    {
        GRoot.inst.SetContentScaleFactor(_designWidth, _designHeight);

		UIPackage.AddPackage("UI/通用组件");
		UIPackage.AddPackage("UI/网络掩码");
		UIPackage.AddPackage("UI/主界面");
		UIPackage.AddPackage("UI/游戏界面");

		///////////////////////////////////////////////////////////////////
		_mainUI = new GComponent ();
		GRoot.inst.AddChild(_mainUI);

		_popupUI = new GComponent ();
		GRoot.inst.AddChild(_popupUI);

		// 初始化调试界面
		DebugInfo.InitDebugView();

		_netMask = UIPackage.CreateObject("网络掩码", "网络掩码").asCom;
		GRoot.inst.AddChild(_netMask);

		///////////////////////////////////////////////////////////////////
		/// UI就绪
		GameController.inst.OnUIReady();
    }

    protected override void DestroyView()
    {
        base.DestroyView();

        UIPackage.RemovePackage("UI/网络掩码");
        UIPackage.RemovePackage("UI/通用组件");
		UIPackage.RemovePackage("UI/主界面");
		UIPackage.RemovePackage("UI/游戏界面");
    }

    public void SwitchTo(UIBaseWindow window)
    {
        if (_curUI != null)
        {
            //GRoot.inst.RemoveChild(_curUI.uiComp);
			_curUI.uiComp.RemoveFromParent();
            _curUI.OnExit();
        }

        _curUI = window;
        if (_curUI != null)
        {
            //GRoot.inst.AddChildAt(_curUI.uiComp, 0);
			_mainUI.AddChild(_curUI.uiComp);
        }
    }

    /// <summary>
    /// 显示弹出窗口
    /// </summary>
    /// <param name="window"></param>
    public void PopupWindow(UIPopupWindow window)
    {
        window.OnClose(() => {
            _curTop -= 1;
        });
        //GRoot.inst.AddChildAt(window.uiComp, _curTop);
		//GRoot.inst.AddChildAt(window.uiComp, Constants.DEBUG_GAME ? 3 : 2);
		_popupUI.AddChild(window.uiComp);
        _curTop += 1;
    }

    public void ShowMask(bool show)
    {
		if (show) {
			DebugInfo.Error ("显示网络标识");
		} else {
			DebugInfo.Error ("隐藏网络标识");
		}
        _netMask.visible = show;
        _netMask.enabled = show;
    }
}
